//
//  ViewController.m
//  testImage
//
//  Created by lianglibao on 2018/7/4.
//  Copyright © 2018年 lianglibao. All rights reserved.
//

#import "ViewController.h"
#import "ShowLargeImageViewController.h"
#import "UIImage+Test.h"

@interface ViewController ()
@property (nonatomic, weak) UIButton *typeOnebtn;
@property (nonatomic, weak) UIButton *typeTwobtn;
@end

@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor lightGrayColor];
    self.title = NSStringFromClass(self.class);
//    [self testScaleImage];
    [self setupSubviews];
}

- (void)setupSubviews {
    [self.view addSubview:({
        UIButton *tBtn = [UIButton new];
        self.typeOnebtn = tBtn;
        [tBtn setTitle:@"方式一\n使用CATiledLayer加载大长图" forState:UIControlStateNormal];
        [tBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        tBtn.backgroundColor = [UIColor whiteColor];
        tBtn.titleLabel.font = [UIFont systemFontOfSize:16];
        tBtn.titleLabel.numberOfLines = 0;
        tBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
        [tBtn sizeToFit];
        [tBtn addTarget:self action:@selector(loadLargeMapImageType:) forControlEvents:UIControlEventTouchUpInside];
        tBtn.frame = (CGRect){150, 100, tBtn.frame.size};
        tBtn;
    })];
    
    [self.view addSubview:({
        UIButton *tBtn = [UIButton new];
        self.typeTwobtn = tBtn;
        [tBtn setTitle:@"方式二\n使用切割长图加载" forState:UIControlStateNormal];
        [tBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        tBtn.backgroundColor = [UIColor whiteColor];
        tBtn.titleLabel.font = [UIFont systemFontOfSize:16];
        tBtn.titleLabel.numberOfLines = 0;
        tBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
        [tBtn sizeToFit];
        [tBtn addTarget:self action:@selector(loadLargeMapImageType:) forControlEvents:UIControlEventTouchUpInside];
        tBtn.frame = (CGRect){450, 100, tBtn.frame.size};
        tBtn;
    })];
}

- (void)loadLargeMapImageType:(UIButton *)btn {
    ShowLargeImageViewController *showLargeImageVC = [ShowLargeImageViewController new];
    if ([btn isEqual:self.typeOnebtn]) {
        showLargeImageVC.loadLargeType = kLoadLargeImageTypeUseTileLayer;
        [self.navigationController pushViewController:showLargeImageVC animated:YES];
    } else if ([btn isEqual:self.typeTwobtn]) {
        showLargeImageVC.loadLargeType = kLoadLargeImageTypeUseClipLargeImage;
        [self.navigationController pushViewController:showLargeImageVC animated:YES];
    } else {
        // NOTHING..
    }
}

- (void)testScaleImage {
    // 200 * 300 像素
    UIImage *sourceImage = [UIImage imageNamed:@"test_scale.jpg"];
    // 方法1:直接缩放到100像素宽
    UIImage *scaleWidthImage = [sourceImage scaledImageToWidth:100];
    NSLog(@"scaleWidthImage=%@ \n scaleWidthImage_scale = %f", scaleWidthImage, scaleWidthImage.scale);
    
    // 方法2:保证最短边缩放到100像素宽
    UIImage *scaleSizeImage = [sourceImage scaledImageToSize:CGSizeMake(100, 100)];
    NSLog(@"scaleSizeImage=%@\n scaleSizeImage_scale = %f", scaleSizeImage, scaleSizeImage.scale);
    
    // 方法3:对比宽高比
    UIImage *scaleWHImage = [sourceImage scaleImageToSize:CGSizeMake(100, 100)];
    NSLog(@"scaleWHImage=%@\n scaleWHImage = %f", scaleWHImage, scaleWHImage.scale);
}
@end

